/*
 * Created: 2022-08-27 09:58:02
 * Author : 涛涛涛
 */

import { createContext, FunctionComponentElement, useCallback, useEffect, useState } from 'react';
import Operation, { IProps as OperationProps } from './Operation/Operation';
import { Product_Web } from '@/pages/_server/_class/Product/Product';
import { apiGetProductById } from '@/pages/_server/_api/ApiProduct/apiGetProductById';
import Taro, { useRouter } from '@tarojs/taro';
export type ProductExhibitionContextType = {
    /** 操作按钮 */
    Operation: FunctionComponentElement<OperationProps>;
    /** 商品信息 */
    product: Product_Web;
    /** 获取/刷新商品信息 */
    freshProduct: () => void;
};
export const ProductExhibitionContext = createContext<ProductExhibitionContextType>(undefined);

/** 使用产品详细信息 */
export const useProductInfomation = () => {
    const [product, setProduct] = useState<Product_Web>(new Product_Web());
    const route = useRouter();
    const freshProduct = useCallback(() => {
        apiGetProductById({ productId: route.params.productId }).then((prodRes) => {
            setProduct(prodRes.data);
        });
    }, [route.params.productId]);
    useEffect(() => {
        freshProduct();
    }, [freshProduct]);
    return {
        product,
        freshProduct,
    };
};
